﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Controls;


namespace VAdvantage.Grids
{
    public class VDataGridLobColumn : DataGridBoundColumn, IDataGridColumn
    {
        private TextBlock textBlock = null;
        private GridField _field = null;
        private String _columnName = "";

        private IControl editingControl = null;
        private object currentCellValue;

        private int _displayType = Classes.DisplayType.Binary;

        System.Windows.Data.Binding binding = null;

        public string DataPropertyName
        {
            get
            {
                return _columnName;
            }
            set
            {
                _columnName = value.ToUpper();

                binding = new System.Windows.Data.Binding(_columnName);
                binding.Converter = new BinaryToTextConvertor();
            }
        }

        public Controls.VDataGrid DataGridView
        {
            get;
            set;
        }

        public GridField Field
        {
            get
            {
                return _field;
            }
            set
            {
                _field = value;
                _displayType = _field.GetDisplayType();


            }
        }

        public void Dispose()
        {
            if (editingControl != null)
            {
                editingControl.Dispose();
            }
            textBlock = null;
            editingControl = null;
            _field = null;
        }

        public bool ReadOnly
        {

            get;
            set;

        }
        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            if (editingControl == null)
            {
                if (_displayType == Classes.DisplayType.Binary)
                {
                    editingControl = new VBinary(_field.GetColumnName(), _field.GetWindowNo());
                     editingControl.SetField(_field);
                }
                else 
                {
                    editingControl = new VTextBox();
                }
               
            }

            currentCellValue = ((DataUtil.DataObject)dataItem).GetFieldValue(_columnName);

            textBlock = (TextBlock)cell.Content;
            //editingControl.SetValueInDataGridColumn(null);

            return (FrameworkElement)editingControl;
        }


        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            if (cell.Content is VBinary)
            {
                editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);

                currentCellValue = editingControl.GetValue();
                _field.SetValue(currentCellValue);
                ((DataUtil.DataObject)dataItem).SetFieldValue(_columnName, currentCellValue);
                //editingControl.SetValue(null);
                //editingControl                //currentBlock.Text = value == null ? "0" : value.ToString();
                //textBlock.Text = currentCellValue == null ? "" : currentCellValue.ToString();
                return textBlock;
            }
            else if (cell.Content is VTextBox)
            {
                editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);

                currentCellValue = editingControl.GetValue();
                _field.SetValue(currentCellValue);
                ((DataUtil.DataObject)dataItem).SetFieldValue(_columnName, currentCellValue);
                //editingControl.SetValue(null);
                //editingControl                //currentBlock.Text = value == null ? "0" : value.ToString();
                //textBlock.Text = currentCellValue == null ? "" : currentCellValue.ToString();
                return textBlock;


            }

            TextBlock tb = Utility.AEnv.GetDataGridElement();

            if (binding != null)
            {
                tb.SetBinding(TextBlock.TextProperty, binding);
            }
            return tb;
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            //if (editingEventArgs.OriginalSource is TextBlock)
            //{
            //    textBlock = (TextBlock)editingEventArgs.OriginalSource;
                if (editingControl is VBinary)
                {
                    ((IEditingControl)editingControl).EditingControlDataGridView = DataGridView;
                    ((IEditingControl)editingControl).SetValueInDataGridColumn(currentCellValue);
                }
                //editingControl.VetoableChangeListener += new EventHandler(editingControl_VetoableChangeListener);
            //}
            return editingElement;
        }

        void editingControl_VetoableChangeListener(object sender, EventArgs e)
        {
            DataGridView.RaiseCellValueChanged(sender);
            editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);
        }

    }
}
